using System;
using System.Data;
using istIUniDB;
using System.Collections;
using System.Data.OleDb;

namespace Shraga.BLL
{
	/// <summary>
	/// Represents an addressee of note reply.
	/// </summary>
	[DBFunctions("shraga_addressee.get_addressee","")]
	public class Addressee : DBObject
	{
		#region Access Methods
		
		/// <summary>
		/// Access method, get only.
		/// </summary>
		public int AddresseeId
		{
			get { return base.PrimaryKey; }
		}

		/// <summary>
		/// Access method, get only.
		/// </summary>
		public int ReplyId
		{
			get { return base.GetIntData("REPLY_ID"); }
		}

		/// <summary>
		/// Access method, get only.
		/// </summary>
		public int UserId
		{
			get { return base.GetIntData("USER_ID"); }
		}

		/// <summary>
		/// Access method, get.
		/// </summary>
		public bool IsRead
		{
			get { return base.GetBoolData("IS_READ"); }		
		}		
		
		/// <summary>
		/// Access method, get only (loads only in the first time).
		/// </summary>
		public String UserName
		{			
			get { return base.GetStringData("USER_NAME"); }
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Constructs the addressee from the DB.
		/// </summary>
		public Addressee(int AddresseeId)
		:base(AddresseeId)
		{}

		/// <summary>
		/// Constructs the addressee from raw data.
		/// </summary>
		/// <param name="NoteID"></param>
		/// <param name="ReplyID"></param>
		/// <param name="UserID"></param>
		/// <param name="IsCC"></param>
		/// <param name="IsBCC"></param>
		/// <param name="Read"></param>
		public Addressee(DataRow RawData)
		: base(RawData)
		{}

		/// <summary>
		/// Creates a new reply addressee and stores in the db (marks as unread by default).
		/// </summary>
		/// <param name="NoteID"></param>
		/// <param name="ReplyID"></param>
		/// <param name="UserID"></param>
		/// <param name="IsCC"></param>
		/// <param name="IsBCC"></param>
		public Addressee(int ReplyId, int UserId)
		{
			#region Inserts the new addressee

			IUniDB udb = UniDBSingleton.Instance().UniDB;						
			
			// Creates the parameters for the insert 
			clsParameter[] arrParams = new clsParameter[2];
			arrParams[0] = new clsParameter(ReplyId, OracleTypes.Int32);
			arrParams[1] = new clsParameter(UserId, OracleTypes.Int32);
			
			// Inserts the new addressee
			DataTable dtNew = udb.RunStoredFunctionReturnDataTable("shraga_addressee.Create_Addressee", ref arrParams);
			
			#endregion

			base.LoadDataRow(dtNew.Rows[0]);
		}

		#endregion

		#region Addressee Methods

		public void MarkAsRead()
		{
			if (!IsRead)
			{
				IUniDB udb = UniDBSingleton.Instance().UniDB;						
			
				// Creates the parameters for the select 
				clsParameter[] arrParams = new clsParameter[1];
				arrParams[0] = new clsParameter(AddresseeId, OracleTypes.Int32);
			
				// Updates the read to true
				udb.RunStoredProcedure("Shraga_Adressee.Mark_As_Read", ref arrParams);

				base.RawData["IS_READ"] = 1;
			}
		}

		#endregion		
	}
}
